package com.daiqee.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.daiqee.controller.support.CommonsController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.GoodsList;
import com.daiqee.service.impl.GoodsListService;

/**
 * 货物清单
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:53:52
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("repository/goods-list")
public class GoodsListController extends CommonsController<GoodsList>{
	
	@Autowired
	private GoodsListService service;
	
	/**
	 * 扩展默认的GET请求方法
	 */
	@Override
	public Object warpperQueryMethod(GoodsList e,String dateScope,EntityWrapper<GoodsList> ew,Page<GoodsList> page) {
		ew.eq("goods_list_in_stock_status", "入库完成");
		ew.and();
		ew.eq("goods_list_out_flag", "N");
		return service.selectPage(page, ew);
	}
	
	/**
	 * PUT请求扩展
	 */
	@Override
	public boolean warpperUpdateMethod(GoodsList goods) {
		goods.setGoodsListOutStockCount(goods.getGoodsListProductAmount());
		return service.updateById(goods);
	}
	
	/**
	 * 扩展默认的POST请求方法
	 */
	@Override
	public boolean warpperAddMethod(GoodsList goods) {
		//先查询该货物是否存在
		EntityWrapper<GoodsList> ew = new EntityWrapper<GoodsList>();
		ew.eq("goods_list_product_name", goods.getGoodsListProductName());
		ew.eq("goods_list_in_stock_status", "入库完成");
		
		//查询货物
		GoodsList haveGoods = service.selectOne(ew);
		goods.setGoodsListInStockTime(new Date());
		
		//该货物存在
		if(haveGoods != null) {
			//合并值添加
			haveGoods.setGoodsListProductAmount(
					haveGoods.getGoodsListProductAmount() + goods.getGoodsListProductAmount());
			haveGoods.setGoodsListInStockTime(goods.getGoodsListInStockTime());
			haveGoods.setGoodsListOperatLog("累计添加-操作用户:"+getCurrentUser().getUseAuthor());
			isNormalFinish = service.updateById(haveGoods);
		}else {
			//直接添加
			goods.setGoodsListOutStockCount(goods.getGoodsListProductAmount());
			goods.setGoodsListInStockStatus("入库完成");
			goods.setGoodsListInFlag("N");
			goods.setGoodsListOperatLog("该货物直接入库,没有指定入库单-操作用户:"+getCurrentUser().getUseAuthor());
			isNormalFinish = service.insert(goods);
		}
		return isNormalFinish;
	}
	
	/**
	 * 转移货物,适用整个货架的货物转移和单个货物转移<br/>
	 * 这里单个货物和整个货物转移 只能有一条进行,优先执行单个货物转移
	 * 
	 * @param origin
	 * 			需要被替换掉的货物所属的货架id
	 * 
	 * @param target
	 * 			要转移到的货架的id
	 * 
	 * @param goodsId
	 * 			单个货物转移的id
	 * 
	 * @return restful 风格 Http状态码
	 */
	@RequestMapping(value = "/move-goods",method=RequestMethod.PUT)
	public Object updateGoodsBelongToStorage(String origin,String target,String goodsId){
		EntityWrapper<GoodsList> ew = new EntityWrapper<GoodsList>();
		if(goodsId != null && !(StringUtils.isBlank(goodsId))) {
			//传入的货物id
			ew.eq("goods_list_product_id", goodsId);
		}else if(origin != null && !(StringUtils.isEmpty(origin))) {
			//货架id等于传入的id的要被更新
			ew.eq("goods_list_storage_id", origin);
		}else {
			//都没有传入进来的情况
			return ResponseEntity.badRequest().build();
		}
		//创建更新的值
		GoodsList storageValue = new GoodsList();
		//设置新的货架id值
		storageValue.setGoodsListStorageId(target);
		//更新
		isNormalFinish =  service.update(storageValue, ew);
		return restfulResponse.response(isNormalFinish,RequestType.PUT);
	}
	
}
